package com.hxn.wxshake.utils;

import java.util.*;
import java.util.stream.Stream;

/**
 * @ClassName MapSort
 * @Description 对map进行排序  根据value
 * @Author hxn
 * @Date 2019/7/25 9:05
 * @Version 1.0
 **/
public class MapSort {

    /**
     * @Author hxn
     * @Description 对map进行升序排列
     * @Param
     * @return
     **/
    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
        Map<K, V> result = new LinkedHashMap<>();
        Stream<Map.Entry<K, V>> st = map.entrySet().stream();
        st.sorted(Comparator.comparing(Map.Entry<K, V>::getValue)).forEach(e -> result.put(e.getKey(), e.getValue()));
        return result;
    }
    /**
     * @Author hxn
     * @Description 对map进行降序排列
     * @Param
     * @return
     **/
    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValueByDesc(Map<K, V> map) {
        Map<K, V> result = new LinkedHashMap<>();
        Stream<Map.Entry<K, V>> st = map.entrySet().stream();
        st.sorted(Comparator.comparing(Map.Entry<K, V>::getValue).reversed()).forEach(e -> result.put(e.getKey(), e.getValue()));
        return result;
    }

    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValueByDescSubStringKey(Map<K, V> map) {
        Map<String, V> result = new LinkedHashMap<>();
        Stream<Map.Entry<K, V>> st = map.entrySet().stream();
        st.sorted(Comparator.comparing(Map.Entry<K, V>::getValue).reversed()).forEach(e -> result.put(((String)e.getKey()).substring(((String)e.getKey()).indexOf("wxshake")+7), e.getValue()));
        return (Map<K, V>) result;
    }


}
